define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/property/index' + location.search,
                    // add_url: 'store/property/add',
                    // edit_url: 'store/property/edit',
                    // del_url: 'store/property/del',
                    // multi_url: 'store/property/multi',
                    // import_url: 'store/property/import',
                    table: 'wanlshop_shop',
                },
                dragsort: false,
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                Search:true,
                commonSearch:true,
                searchFormVisible:true,
                columns: [
                    [
                       // {checkbox: true},
                       // {field: 'id', title: __('Id')},
                      //  {field: 'user_id', title: __('User_id')},
                        {field: 'id', title: __('店铺'),operate: 'IN',visible:false},
                        {field: 'shopname', title: __('Shopname'), operate: false},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'keywords', title: __('Keywords'), operate: 'LIKE'},
                       // {field: 'description', title: __('Description'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                       // {field: 'service_ids', title: __('Service_ids'), operate: 'LIKE'},
                       // {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                      //  {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'level', title: __('Level')},
                        // {field: 'islive', title: __('Islive')},
                        // {field: 'isself', title: __('Isself')},
                        {field: 'city', title: __('City'), operate: 'LIKE'},
                        {field: 'c1', title: __('储值余额'), operate: false},
                        {field: 'c3', title: __('储值已提现'), operate: false},
                        {field: 'c5', title: __('储值可提现'), operate: false},
                        {field: 'c2', title: __('账户余额'), operate: false},
                        {field: 'c4', title: __('账户已提现'), operate: false},
                        {field: 'c6', title: __('账户可提现'), operate: false},

                        // {field: 'return', title: __('Return'), operate: 'LIKE'},
                        // {field: 'like', title: __('Like')},
                        // {field: 'score_describe', title: __('Score_describe'), operate:'BETWEEN'},
                        // {field: 'score_service', title: __('Score_service'), operate:'BETWEEN'},
                        // {field: 'score_deliver', title: __('Score_deliver'), operate:'BETWEEN'},
                        // {field: 'score_logistics', title: __('Score_logistics'), operate:'BETWEEN'},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        // {field: 'verify', title: __('Verify'), searchList: {"0":__('Verify 0'),"1":__('Verify 1'),"2":__('Verify 2'),"3":__('Verify 3'),"4":__('Verify 4')}, formatter: Table.api.formatter.normal},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // 
                        // {field: 'code', title: __('Code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        
                        // {field: 'auth_id', title: __('Auth_id')},
                        // {field: 'person', title: __('Person'), operate: 'LIKE'},
                        // {field: 'telephone', title: __('Telephone'), operate: 'LIKE'},
                        // {field: 'staff_id', title: __('Staff_id')},
                        // {field: 'maturitytime', title: __('Maturitytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'payment', title: __('Payment'), operate:'BETWEEN'},
                        // {field: 'settled', title: __('Settled'), operate:'BETWEEN'},
                        // {field: 'adds', title: __('Adds'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate,
                            buttons: 
                                [{
                                    name: 'detail',
                                    title: __('明细'),
                                    text: __('明细'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: '',
                                    url:  function (row, index, fields) {
                                        // 动态生成跳转URL，并添加参数
                                        return 'store/property/detail?shop_id='+row.id;
                                    }
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                onPostCommonSearch: function (event, table) {
                    //动态下拉列表
                    $("form.form-commonsearch input[name='id']")
                    .addClass("selectpage")
                    .data("multiple","true")
                    .data("field", "shopname")
                    .data("source", "store/shop/index");
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'store/property/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'store/property/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'store/property/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        detail:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/property/detail'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    {field: 'transaction_id', title: '交易ID', width: 100},
                    {field: 'type', title: '交易类型', width: 80, formatter: function(value) {
                    return value === 'income' ? '<span class="text-success">收入</span>' : '<span class="text-danger">支出</span>';
                    }},
                    {field: 'transaction_time', title: __('交易时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    {field: 'transaction_code', title: '交易流水号', width: 180},
                    {field: 'orderno', title: '订单号', width: 150},
                    {field: 'formatted_amount', title: '金额', width: 100, formatter: function(value, row) {
                    return row.type === 'income' 
                        ? '<span class="text-success">' + value + '</span>' 
                        : '<span class="text-danger">' + value + '</span>';
                    }},
                    {field: 'fee_amount', title: '手续费', width: 100},
                    {field: 'abalance', title: '结余账户', width: 100},
                    {field: 'bbalance', title: '佣金账户', width: 100}
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
